package com.nycreativity.elecreativity.auth;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @description 基于角色授权切面
 * @author ttxxi
 * @create 2021/10/11 21:19
 */
@Aspect
@Component
public class RoleAuthAspect {

    {
        System.out.println("==================================基于角色认证切面添加成功===================================");
    }

    /**
     * 定义切面
     */
    @Pointcut(value = "@annotation(com.nycreativity.elecreativity.auth.RequireRole)")
    public void pointCut(){

    }

    /**
     * 获取注解的值并使用AuthStrategy工具类验证，未通过则抛异常
     * @param requireRole 具体注解的对象
     */
    @Before(value = "pointCut() && @annotation(requireRole)")
    public void authRole(RequireRole requireRole){
        String[] roles = requireRole.value();
        AuthStrategy authStrategy = requireRole.authStrategy();
        AuthUtil.requireRoles(authStrategy, roles);
    }

}

